﻿using TrueSync;
using System.Collections.Generic;
using Framework;

public class LogicEventComponent : LogicComponent
{
    LogicEntity mLEntity;
    Dictionary<int, Event> mEvents;//已经加载的事件
    Dictionary<int, Event> mCallbackEvents;//已经加载的回调事件

    Dictionary<int, Queue<Event>> mEventBuffer;

    public void Init(LogicEntity LEntity)
    {
        mLEntity = LEntity;
        mEvents = new Dictionary<int, Event>(); 
        mCallbackEvents = new Dictionary<int, Event>();
        mEventBuffer = new Dictionary<int, Queue<Event>>();
    }

    public void Start()
    {

    }

    public void DoUpdate(FP deltaTime)
    {
        for(int i = 0; i < mEvents.Count; i++)
        {
            mEvents[i].Tick(deltaTime);
        }
    }

    public void Destroy()
    {
        foreach (var aevent in mEvents.Values)
        {
            aevent.OnDestroy();
        }
        mEventBuffer = null;
        mEvents = null;
        mCallbackEvents = null;
    }

    //子物体或者技能流开始的时候注册
    public void SetEvent(List<int> eventIDArray)
    {
        for(int i = 0; i < eventIDArray.Count; i++)
        {
            Event aevent = GetEvent(eventIDArray[i]);
            aevent.Init(mLEntity.self);
            mEvents[eventIDArray[i]] = aevent;
        }
    }

    //子物体或者技能流结束的时候销毁
    public void DestroyEvent(List<int> eventIDArray)
    {
        for (int i = 0; i < eventIDArray.Count; i++)
        {
            Event tobeRemovedEvent = mEvents[eventIDArray[i]];
            tobeRemovedEvent.OnDestroy();
            ReturnEvent(tobeRemovedEvent);
            mEvents.Remove(eventIDArray[i]);
            UnregisterCallbackEvent(tobeRemovedEvent);
        }
    }

    public Event GetEvent(int eventID)
    {
        Event requiredEvent = null;
        if(mEventBuffer.ContainsKey(eventID) && mEventBuffer[eventID].Count > 0)
        {
            requiredEvent = mEventBuffer[eventID].Dequeue();
        }

        if(requiredEvent == null)
        {
            EByte info = new EByte();
            requiredEvent = new Event();
            requiredEvent.Deserialize(info);
        }

        return requiredEvent;
    }

    void ReturnEvent(Event aevent)
    {
        if (mEventBuffer.ContainsKey(aevent.ID) == false)
            mEventBuffer[aevent.ID] = new Queue<Event>();
        mEventBuffer[aevent.ID].Enqueue(aevent);
    }

    public void RegisterCallbackEvent(Event callbackEvent)
    {
        if(mCallbackEvents.ContainsKey(callbackEvent.ID) == false)
        {
            mCallbackEvents[callbackEvent.ID] = callbackEvent;
        }
    }

    public void UnregisterCallbackEvent(Event callbackEvent)
    {
        if(mCallbackEvents.ContainsKey(callbackEvent.ID))
        {
            mCallbackEvents.Remove(callbackEvent.ID);
        }
    }

    public void CallEvent(int eventID, params object[] callbackParams)
    {
        if(mCallbackEvents.ContainsKey(eventID) && mCallbackEvents[eventID].Enabled)
        {
            mCallbackEvents[eventID].DoEffect(callbackParams);
        }
    }
}
